'use strict';
const Controller = require('egg').Controller;
const tables = require("../../public/tables.js");

class TableController extends Controller {
    async getTables() {
        this.ctx.body = tables;
    }
    async create() {
        let { ctx, service } = this;
        let { name, total } = ctx.request.body;
        let code = 1, msg = "";
        let endArr = await service.table.create(name, total);
        endArr.forEach(item => {
            if (item.affectedRows == 0) {
                code == 0;
                msg += `;生成${item.tabName}表失败`;
            } else {
                msg += `;生成${item.tabName}表成功`;
            }
        });
        msg = msg.substr(1);
        ctx.body = service.utility.responseBody(code, msg);
    }
    async truncate() {
        let { ctx, service } = this;
        let { name } = ctx.request.body;
        let code = 1, msg = `清空${name}表成功`;
        let end = await service.table.truncate(name);
        if (end.warningCount != 0) {
            code = 0;
            msg = `清空${name}表失败`;
            tips = end.message;
        }
        ctx.body = service.utility.responseBody(code, msg);
    }
}

module.exports = TableController;
